﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DataAccess_Layer
{
    public class History_DAC
    {
        DataClasses_RetailerDataContext context = new DataClasses_RetailerDataContext();
        private static List<SqlParameter> makeInsertParams(History newBonus)
        {
            List<SqlParameter> sqlParams = new List<SqlParameter>();

            SqlParameter inParam = new SqlParameter("@NgayLapHoaDon", SqlDbType.NVarChar, 50);
            inParam.Direction = ParameterDirection.Input;
            inParam.Value = newBonus.NgayLapHoaDon1;
            sqlParams.Add(inParam);

            inParam = new SqlParameter("@TongTien", SqlDbType.NVarChar, 50);
            inParam.Direction = ParameterDirection.Input;
            inParam.Value = newBonus.TongTien1;
            sqlParams.Add(inParam);

            inParam = new SqlParameter("@ID_Customer", SqlDbType.BigInt);
            inParam.Direction = ParameterDirection.Input;
            inParam.Value = newBonus.ID_Customer1;
            sqlParams.Add(inParam);

            return sqlParams;
        }
      
        public static int insert(History newBonus)
        {
            List<SqlParameter> sqlParams = makeInsertParams(newBonus);

            DataAccessHelper helper = null;
            SqlDataReader rdr = null;
            int count;
            try
            {
                helper = new DataAccessHelper();
                rdr = helper.excuteSP("SP_InsertHistoryCustomer", sqlParams);
            }
            finally
            {
                helper.close();
                rdr.Close();

                // If your Command contains output parameters or return values, they will not be available until the DataReader is closed.            
                count = (int)sqlParams[sqlParams.Count - 1].Value;
            }

            return count;
        }
        //History detail
        private static List<SqlParameter> makeInsertParamsDetails(HistoryDetail newBonus)
        {
            List<SqlParameter> sqlParams = new List<SqlParameter>();

            SqlParameter inParam = new SqlParameter("@ID_LichSuMuaHang", SqlDbType.Int, 50);
            inParam.Direction = ParameterDirection.Input;
            inParam.Value = newBonus.ID_LichSu1;
            sqlParams.Add(inParam);

            inParam = new SqlParameter("@TenSanPham", SqlDbType.NVarChar, 50);
            inParam.Direction = ParameterDirection.Input;
            inParam.Value = newBonus.TenSanPham1;
            sqlParams.Add(inParam);

            inParam = new SqlParameter("@SoLuong", SqlDbType.BigInt);
            inParam.Direction = ParameterDirection.Input;
            inParam.Value = newBonus.SoLuong1;
            sqlParams.Add(inParam);

            inParam = new SqlParameter("@GiaTien", SqlDbType.BigInt);
            inParam.Direction = ParameterDirection.Input;
            inParam.Value = newBonus.GiaTien1;
            sqlParams.Add(inParam);

            inParam = new SqlParameter("@ThanhTien", SqlDbType.BigInt);
            inParam.Direction = ParameterDirection.Input;
            inParam.Value = newBonus.ThanhTien1;
            sqlParams.Add(inParam);

            return sqlParams;
        }
        public static bool insertHistoryDetail(HistoryDetail newBonus)
        {
            List<SqlParameter> sqlParams = makeInsertParamsDetails(newBonus);

            DataAccessHelper helper = null;
            SqlDataReader rdr = null;
            int count;
            try
            {
                helper = new DataAccessHelper();
                rdr = helper.excuteSP("SP_InsertHistoryDetail", sqlParams);
            }
            finally
            {
                helper.close();
                rdr.Close();

                // If your Command contains output parameters or return values, they will not be available until the DataReader is closed.            
                count = (int)sqlParams[sqlParams.Count - 1].Value;
            }

            return (count==1);
        }
        //Get All List
        public static List<History> getAll(int ID_Customer)
        {
            List<SqlParameter> sqlParams = makeGetHistory(ref ID_Customer);
            List<History> bonusDetails = new List<History>();
            History bonusDetail = null;
            DataAccessHelper helper = null;
            SqlDataReader rdr = null;

            try
            {
                helper = new DataAccessHelper();
                rdr = helper.excuteSP("SP_Get_All_History", sqlParams);

                if (rdr.HasRows)
                {
                    while (rdr.Read())
                    {
                        bonusDetail = createHistory(rdr);
                        bonusDetail.ID_Customer1 = ID_Customer;
                        bonusDetails.Add(bonusDetail);
                    }
                }
            }
            finally
            {
                helper.close();
                rdr.Close();
            }

            return bonusDetails;
        }
        private static List<SqlParameter> makeGetHistory(ref int ID_Customer)
        {
            List<SqlParameter> sqlParams = new List<SqlParameter>();
            SqlParameter inParam = new SqlParameter("@ID_Customer", SqlDbType.Int);
            inParam.Direction = ParameterDirection.Input;
            inParam.Value = ID_Customer;
            sqlParams.Add(inParam);
            return sqlParams;
        }
        private static History createHistory(SqlDataReader rdr)
        {
            History cat = new History();
            cat.Id = (int)rdr["ID"];
            cat.TongTien1 = (string)rdr["TongTien"];
            cat.NgayLapHoaDon1 = (string)rdr["NgayLapHoaDon"];
           
            return cat;
        }
        //get all historydetail
        public static List<HistoryDetail> getAllDetail(int ID_LichSu)
        {
            List<SqlParameter> sqlParams = makeGetHistoryDetail(ref ID_LichSu);
            List<HistoryDetail> bonusDetails = new List<HistoryDetail>();
            HistoryDetail bonusDetail = null;
            DataAccessHelper helper = null;
            SqlDataReader rdr = null;

            try
            {
                helper = new DataAccessHelper();
                rdr = helper.excuteSP("SP_Get_All_HistoryDetail", sqlParams);

                if (rdr.HasRows)
                {
                    while (rdr.Read())
                    {
                        bonusDetail = createHistoryDetail(rdr);
                        bonusDetail.ID_LichSu1 = ID_LichSu;
                        bonusDetails.Add(bonusDetail);
                    }
                }
            }
            finally
            {
                helper.close();
                rdr.Close();
            }

            return bonusDetails;
        }
        private static List<SqlParameter> makeGetHistoryDetail(ref int ID_Customer)
        {
            List<SqlParameter> sqlParams = new List<SqlParameter>();
            SqlParameter inParam = new SqlParameter("@IDLichSu", SqlDbType.Int);
            inParam.Direction = ParameterDirection.Input;
            inParam.Value = ID_Customer;
            sqlParams.Add(inParam);
            return sqlParams;
        }
        public static List<HistoryDetail> getAllWithName()
        {
            List<HistoryDetail> products = null;

            DataAccessHelper helper = null;
            SqlDataReader rdr = null;
            try
            {
                helper = new DataAccessHelper();
                rdr = helper.excuteSP("SP_best_sell_products");

                if (rdr.HasRows)
                {
                    products = new List<HistoryDetail>();
                    while (rdr.Read())
                    {
                        HistoryDetail ht = createHistoryDetail1(ref rdr);
                        products.Add(ht);
                    }
                }
            }
            finally
            {
                helper.close();
                rdr.Close();
            }

            return products;
        }


        private static HistoryDetail createHistoryDetail1(ref SqlDataReader rdr)
        {
            HistoryDetail cat = new HistoryDetail();
            cat.TenSanPham1 = (string)rdr["TenSanPham"];

            cat.GiaTien1 = (string)rdr["GiaTien"];

            return cat;
        }
        private static HistoryDetail createHistoryDetail(SqlDataReader rdr)
        {
            HistoryDetail cat = new HistoryDetail();
            cat.ID1 = (int)rdr["ID"];
            cat.TenSanPham1 = (string)rdr["TenSanPham"];
            cat.SoLuong1 = (string)rdr["SoLuong"];
            cat.GiaTien1 = (string)rdr["GiaTien"];
            cat.ThanhTien1 = (string)rdr["ThanhTien"];
            return cat;
        }
    }
}
